import axios from 'axios'

const instance = axios.create({
  timeout: 10000,
  baseURL: (import.meta.env.PROD ? "http://localhost:8080" : "" ) + '/api'
})

instance.interceptors.request.use(
  (config) => {
    // 设置版本号
    const apiVersion  = `v${(config.version || '1')}`
    config.url = `/${apiVersion}${config.url}`

    return config
  }
)

instance.interceptors.response.use(resp => {
  if (resp.status === 200 && resp.data.code === 200) {
    return resp.data;
  } else {
    return Promise.reject(resp.data)
  }
}, (error => {
  console.log("请求失败：", error)
  return Promise.reject(error)
}))

export default instance
